var FindId;
var loadInfo = 0;

layui.use(['layer'], function () {
    layer = layui.layer;
    layer.photos({
        photos: '#layer-photos-demo'
        , anim: 5
    });

    FindId = GetQueryString("FindId");
    $(".comment_list").html('<h3 style="text-indent: 2em;">评论列表</h3>');
    selectFindDetailsInfoByIdPc();
    initComment();
    $(".show_reply_list").click(function () {

    });


    $(document).ready(function () {
        $(document).on('click', '.show_reply_list', function () {

            if ($(this).closest('.comment').find(".reply_list").css("display") != "none") {
                $(this).closest('.comment').find(".show_remain_reply").css("display", "none");
                $(this).html("查看回复");

            } else {
                $(this).closest('.comment').find(".show_remain_reply").css("display", "block");
                $(this).html("收起回复");

            }
            $(this).closest('.comment').find(".reply_list").toggle(500);


        });

        $(document).on('click', '.show_remain_reply', function () {
            $(this).html("已显示全部回复");
            //TODO发送数据

        });

    });
});


function selectFindDetailsInfoByIdPc() {
    $.ajax({
        url: httpurl + "/selectFind",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        data: {
            "id": FindId,
        },
        success: function (data) {
            if (data.rtnCode == 200) {
                var dataInfo = data.data;
                var headimg=(dataInfo.presenters==0?(dataInfo.zspUserEntity==null?"未知":dataInfo.zspUserEntity.headimg):(dataInfo.presenters==1?(dataInfo.brandInfoEntity==null?"":dataInfo.brandInfoEntity.logo):"官方平台"));
                $(".layadmin-homepage-pad-img").html(headimg);
                var title=(dataInfo.presenters==0?(dataInfo.zspUserEntity==null?"未知":dataInfo.zspUserEntity.username):(dataInfo.presenters==1?(dataInfo.brandInfoEntity==null?"":dataInfo.brandInfoEntity.name):"官方平台"));
                $(".layadmin-homepage-font").html(title);
                $(".time").html(dataInfo.intime);
                $(".article_view").html("评论("+dataInfo.commentsnum+")");
                $(".article_comments").html("点赞("+dataInfo.praisenum+")");
                $(".pulisher").html(dataInfo.zspUserEntity == null ? "" : dataInfo.zspUserEntity.username + "<span style='color: blue'>发表</span>");
                $(".article_content p").html(dataInfo.content);
                var split = dataInfo.img.split(";");
                var info = "";
                for (var i = 0; i < split.length; i++) {
                    // info += '<div class="NavLinks">' +
                    //     '            <img layer-src="' + visitimgurl + "/" + split[i] + '' + visitimgurl + "" src="/" alt="图片名">" + split[i] + '' +
                    //     '        </div>';
                }

                $("#layer-photos-demo").html(info);

            }


        }
    });
}

function initComment() {
    $.ajax({
        url: httpurl + "/selectFindCommentByFidPc",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        data: {
            "fid": FindId,
            "page": pageNo,
            "pagesize": pageRecords,
        },
        success: function (data) {

            if (data.rtnCode == 200) {
                var info = "";
                var initdata = data.data;
                var dataInfo = initdata.list;
                totalRecords = initdata.total;
                for (var i = 0; i < dataInfo.length; i++) {
                    var headimg = (dataInfo[i].spUserEntity == null ? "" : (dataInfo[i].spUserEntity.uimg == null ? dataInfo[i].spUserEntity.headimg : visitimgurl + "/" + dataInfo[i].spUserEntity.uimg));
                    var username = (dataInfo[i].spUserEntity == null ? "" : (dataInfo[i].spUserEntity.username == null ? dataInfo[i].spUserEntity.username : dataInfo[i].spUserEntity.username));
                    info += '<div class="comment">' +
                        '            <div class="conmment_details">' +
                        '            <div class="imgdiv">' +
                        '                <img class="imgcss"  src="' + headimg + '"/>' +
                        '            </div>' +
                        '                <div style="float:left;">' +
                        '                    <span class="comment_name">' + username + '</span><span>' + dataInfo[i].newintime + '</span>' +
                        '                </div>' +
                        '                <div class="del">' +
                        '                    <span  class="show_reply_list">查看回复</span>' +
                        '                    <i class="icon layui-icon" >&#xe6c6;赞(' + dataInfo[i].num + ')</i>' +
                        '                    <a class="del_comment" data-id="' + dataInfo[i].id + '" data-info="deleteFindRecordById" onclick="DeleteInfo(this)"> <i class="icon layui-icon" >&#xe640;删除</i></a>' +
                        '                </div>' +
                        '                <div class="comment_content" >' + dataInfo[i].content + '</div>' +
                        '            </div>' +
                        '            <div class="reply_list"  >';
                    var findRecords = dataInfo[i].findRecords;
                    var infos = "";
                    var tempInfo = "查看剩下的回复";
                    if (findRecords.length > 0) {
                        for (var j = 0; j < findRecords.length; j++) {
                            var username = (findRecords[j].spUserEntity == null ? "" : (findRecords[j].spUserEntity.username == null ? findRecords[j].spUserEntity.username : findRecords[j].spUserEntity.username));
                            infos += '                <div class="reply" >' +
                                '                    <span class="reply_name">' + username + '</span>：<span class="reply_content">' + findRecords[j].content + '</span>' +
                                '                    <a  data-id="' + findRecords[j].id + '" data-info="deleteFindRecordById" onclick="DeleteInfo(this)" class="del_reply">' +
                                '                        <i class="icon layui-icon " >    删除</i>' +
                                '                    </a>' +
                                '                </div>';
                        }
                    } else {
                        tempInfo = "沒有任何回复數据";
                    }
                    info += infos + '</div>' +
                        '            <div class="show_remain_reply">' + tempInfo + '</div>' +
                        '            <!--回复列表结束-->' +
                        '        </div>' +
                        '        <hr>';
                }
                if (dataInfo.length < pageRecords) {
                    loadInfo = 1;
                    $(".comment_add_or_last").html("没有更多评论了");
                } else {
                    $(".comment_add_or_last").html("点击加载更多评论");
                }
                $(".comment_list").append(info);


            } else {
                loadInfo = 1;
                $(".comment_add_or_last").html("没有更多评论了");
            }
        }
    });
}

function loadMore() {
    if (loadInfo == 1) {
        top.layer.alert("沒有更多评论了...");
        return;
    }
    pageNo++;
    initComment();
}